package rank1400;

import _object.TreeNode;

/**
 * 1457. 二叉树中的伪回文路径
 */
public class No1457 {
    private int mask = 0;

    public int pseudoPalindromicPaths(TreeNode root) {
        int ans = 0;
        if (root == null) return ans;
        mask ^= 1 << root.val; // 修改 root.val 出现次数的奇偶性
        if (root.left == null && root.right == null) {
            ans = Integer.bitCount(mask) <= 1 ? 1 : 0;
        } else ans = pseudoPalindromicPaths(root.left) + pseudoPalindromicPaths(root.right);
        mask ^= 1 << root.val;
        return ans;
    }
}
